package com.hyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.User;
import com.hyz.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/9 15:07
 */
@RestController
@RequestMapping("/approve")
public class ApproveController {
    @Autowired
    private UserService userService;

    @ApiOperation("部门审批人员获取所有本部门的用户id")
    @PostMapping("/getUsersByDepartment")
    public ResponseResult<List<Long>> getUsersByDepartment(@RequestParam("userId") Long userId) {
        User user = userService.getById(userId);
        if (Objects.isNull(user)) {
            return new ResponseResult<>(400, "查无此人");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .select(User::getId)
                .eq(User::getDepartmentId, user.getDepartmentId());
        List<User> users = userService.list(wrapper);
        if (users.isEmpty()) {
            return new ResponseResult<>(400, "该部门没有任何员工");
        }
        List<Long> userIds = new ArrayList<>();
        for (User user1 : users) {
            userIds.add(user1.getId());
        }
        return new ResponseResult<>(200, "该部门所有员工id", userIds);
    }
}
